﻿using ReactReduxTool.Model;
using ReactReduxTool.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReactReduxTool.app {

    class AppCtx {

        public delegate void OnInitListener(List<ConfigItemInfoEntry> configs);

        /// <summary>
        /// 初始化应用程序
        /// </summary>
        /// <param name="listener"></param>
        public static void Init(OnInitListener listener) {
            ConfigUtil configUtil = ConfigUtil.GetInstance();
            listener(configUtil.GetConfigs());
        }

        public delegate void OnFinishConfigListener();

        /// <summary>
        /// 完成配置任务
        /// </summary>
        /// <param name="targetDiectory"></param>
        /// <param name="listener"></param>
        public static void FinishConfigTask(string targetDiectory, OnFinishConfigListener listener) {
            new Thread(() => {
                string filesPath = Application.StartupPath + "/../../assets/files";//资源目录
                string targetPath = targetDiectory;//目标目录
                string appPath = targetPath + "/app";//APP目录
                if (!Directory.Exists(appPath))
                    Directory.CreateDirectory(appPath);//创建APP目录
                string babelrcFile = targetPath + "/.babelrc";
                if (!File.Exists(babelrcFile))
                    File.Copy(filesPath + "/.babelrc", babelrcFile);
                string serverFile = targetPath + "/server.js";
                if (!File.Exists(serverFile))
                    File.Copy(filesPath + "/server-js.txt", serverFile);
                string indexJsFile = targetPath + "/app/index.js";
                if (!File.Exists(indexJsFile))
                    File.Copy(filesPath + "/index-js.txt", indexJsFile);
                string webpackConfigJsFile = targetPath + "/webpack.config.js";
                if (!File.Exists(webpackConfigJsFile))
                    File.Copy(filesPath + "/webpack.config-js.txt", webpackConfigJsFile);
                string buildPath = targetPath + "/build";
                if (!Directory.Exists(buildPath))
                    Directory.CreateDirectory(buildPath);
                string indexHtmlFile = buildPath + "/index.html";
                if (!File.Exists(indexHtmlFile))
                    File.Copy(filesPath + "/index-html.txt", indexHtmlFile);
                listener();
            }).Start();
        }

    }
}
